//
//  PPPPTimelinePictureStatusView.m
//  pickpic
//
//  Created by Jinxiao on 10/6/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPTimelinePictureStatusView.h"

#import "PPActivityCommitManager.h"

@interface PPTimelinePictureStatusView ()
@property (readwrite, nonatomic, strong) UIVisualEffectView *blurView;
@property (strong, nonatomic) IBOutlet UIButton *likeButton;
@property (strong, nonatomic) IBOutlet UIButton *commentButton;
@property (strong, nonatomic) IBOutlet DGAlignmentButton *likeCountButton;
@property (strong, nonatomic) IBOutlet DGAlignmentButton *likeCountButton2;
@property (strong, nonatomic) IBOutlet DGAlignmentButton *commentCountButton;
@end

@implementation PPTimelinePictureStatusView

- (void)awakeFromNib {
    [super awakeFromNib];

    _blurView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
    [self insertSubview:_blurView atIndex:0];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    _blurView.frame = self.bounds;
}

- (void)setPicture:(PPTimelinePictureObject *)picture {
    _picture = picture;

    _likeButton.selected = picture.isLiked;

    [_likeCountButton setTitle:[NSString stringWithFormat:@"%li", picture.likeCount] forState:UIControlStateNormal];
    [_likeCountButton2 setTitle:[NSString stringWithFormat:@"%li", picture.likeCount] forState:UIControlStateNormal];
    [_commentCountButton setTitle:[NSString stringWithFormat:@"%li", picture.commentCount] forState:UIControlStateNormal];
    _commentCountButton.hidden = (picture.commentCount > 0) ? NO : YES;
    _likeCountButton2.hidden = (picture.likeCount > 0 && _commentCountButton.hidden == YES) ? NO : YES;
    _likeCountButton.hidden = (picture.likeCount > 0 && _likeCountButton2.hidden == YES) ? NO : YES;

}

- (IBAction)likeButtonDidPressed:(UIButton *)sender {

    if(_likeButton.selected == YES) {
        [MobClick event:@"159"];
        _likeButton.selected = NO;

        _picture.likeCount --;
        _picture.isLiked = NO;

        [[PPActivityCommitManager sharedInstance] offlineUnlikeImageWithImageID:_picture.imageID.integerValue];
    } else {
        [MobClick event:@"158"];
        _likeButton.selected = YES;
        _picture.likeCount ++;
        _picture.isLiked = YES;
        [[PPActivityCommitManager sharedInstance] offlineLikeImageWithImageID:_picture.imageID.integerValue];
    }

    [_likeCountButton setTitle:[NSString stringWithFormat:@"%li", _picture.likeCount] forState:UIControlStateNormal];
    [_likeCountButton2 setTitle:[NSString stringWithFormat:@"%li", _picture.likeCount] forState:UIControlStateNormal];
    _likeCountButton2.hidden = (_picture.likeCount > 0 && _commentCountButton.hidden == YES) ? NO : YES;
    _likeCountButton.hidden = (_picture.likeCount > 0 && _likeCountButton2.hidden == YES) ? NO : YES;
}

@end
